package tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map.Entry;

public class ExperimentsParameters {
	private HashMap<String, String> stock;
	
	public ExperimentsParameters(){
		stock = new HashMap<String, String>();
	}
	
	public void addFromFile(String filename){
		try{
//			read the configure file
			//String split = "\t";
			//String config = "config.txt";
			FileReader fr = new FileReader(new File(filename));
			BufferedReader in = new BufferedReader(fr);
			
			String buf = in.readLine();
			while(buf != null){
				if(buf.indexOf("#")>=0 || buf.length()<2){
					buf = in.readLine();
					continue;
				}
				//buf = buf.replaceAll("[:]", "");
				buf = buf.replaceAll("[\t]", " ");
				buf = buf.replaceAll(" {2,}", " ");
				int pos = buf.indexOf(" ");
				
				String key = buf.substring(0,pos);
				String value = buf.substring(pos+1);
				
				stock.put(key, value);
				
				buf = in.readLine();
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * return null if key is not found 
	 * @param key : request
	 * @return
	 */
	public String get(String key){
		return stock.get(key);	
	}
	
	/**
	 * return null if key is not found or not Integer
	 * @param key : request
	 * @return
	 */
	public Integer getAsInt(String key){
		String res = stock.get(key);
		if(res == null)
			return null;
		try{
			return Integer.parseInt(res);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * return null if key is not found or not Double
	 * @param key : request
	 * @return
	 */
	public Double getAsDouble(String key){
		String res = stock.get(key);
		if(res == null)
			return null;
		try{
			return Double.parseDouble(res);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * return null if key is not found or not Integer[]
	 * @param key : request
	 * @return
	 */
	public Integer[] getAsIntVector(String key){
		String res = stock.get(key);
		if(res == null)
			return null;
		try{
			
			String[] vecString = res.split("[ \t]");
			Integer[] vector = new Integer[vecString.length];
			for(int i=0; i<vecString.length; i++){
				vector[i] = Integer.parseInt(vecString[i]);
			}
			
			return vector;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * return null if key is not found or not Integer[]
	 * @param key : request
	 * @return
	 */
	public Double[] getAsDoubleVector(String key){
		String res = stock.get(key);
		if(res == null)
			return null;
		try{
			
			String[] vecString = res.split("[ \t]");
			Double[] vector = new Double[vecString.length];
			for(int i=0; i<vecString.length; i++){
				vector[i] = Double.parseDouble(vecString[i]);
			}
			
			return vector;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public String toString(){
		String res="";
		for(Entry<String, String> s:stock.entrySet()){
			res+=s.getKey()+" "+s.getValue()+"\n";
		}
		
		return res;
	}

	public Boolean getAsBoolean(String key) {
		String res = stock.get(key);
		if(res == null)
			return null;
		try{
			int i= Integer.parseInt(res);
			if(i==0 || i==-1)
				return false;
			else return true;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
